﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ZombieGame
{
    public class GUIButton : GUIItem
    {
        public string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                var m = ZombieGame.Font1.MeasureString(_text);
                Origin = m / 2f;
                Size = m;
            }
        }
        private string _text;
        public Action<GUIButton> OnClick;
        bool pressed;

        public GUIButton(Vector2 pos, string text)
            : base()
        {
            Position = pos;
            Text = text;
        }

        public override void Draw(SpriteBatch batch)
        {
            batch.DrawString(ZombieGame.Font1, Text, Position, Color, Rotation, Origin, Scale, SpriteEffects.None, 0f);
        }

        public override void Update(GameTime time)
        {
            Vector2 m = new Vector2(InputHandler.CurrentMouseState.X, InputHandler.CurrentMouseState.Y);
            if (m.X > Bounds.Left && m.X < Bounds.Right && m.Y > Bounds.Top && m.Y < Bounds.Bottom)
            {
                if (InputHandler.MouseLPress())
                    pressed = true;
                else
                    if (InputHandler.MouseLRelease() && pressed)
                        if (ImageData.Count > 0)
                        {
                            Vector2 v = ScreenToClient(m);
                            if (ImageData[CurrentFrame][(int)v.X, (int)v.Y].A > 0)
                                OnClick(this);
                        }
                        else
                            OnClick(this);
            }
            base.Update(time);
        }


        public override float Layer
        {
            get { return 0f; }
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
